USE [texchange_db]
GO

/****** Object:  UserDefinedFunction [dbo].[Fn_GetAllPlaces]    Script Date: 05/29/2011 19:55:14 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[Fn_GetAllPlaces] 
(	
	-- Add the parameters for the function here
	@start  bigint=0,
	@limit bigint=30,
	@order nvarchar(10) = 'id',
	@orderdir nvarchar(4) = 'desc',
	@type bigint=1
)
RETURNS TABLE 
AS
RETURN 
(
	-- Add the SELECT statement with parameter references here
	SELECT [id], [short], [name], [isActive], [country_id], [country] from
			(SELECT Places.*, ROW_NUMBER() OVER (ORDER BY Places.id asc) AS  rownum, Pl.name as [country] FROM Places
				LEFT JOIN Places as Pl on  Places.country_id=Pl.id where Places.placetype_id = @type) as a
		WHERE rownum >=@start AND rownum<=@limit
)

GO


